package com.centsuse.my_redis.controller;

import com.centsuse.my_redis.es.entity.DocumentEntity;
import com.centsuse.my_redis.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/documents")
public class DocumentController {
    @Autowired
    private DocumentService documentService;

    @PostMapping
    public ResponseEntity<DocumentEntity> createDocument(@RequestBody DocumentEntity document) {
        DocumentEntity savedDocument = documentService.saveDocument(document);
        return ResponseEntity.ok(savedDocument);
    }

    @GetMapping("/{id}")
    public ResponseEntity<DocumentEntity> getDocumentById(@PathVariable String id) {
        Optional<DocumentEntity> document = documentService.findDocumentById(id);
        return document.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/task/{taskId}")
    public ResponseEntity<List<DocumentEntity>> getDocumentsByTaskId(@PathVariable String taskId) {
        List<DocumentEntity> documents = documentService.findDocumentsByTaskId(taskId);
        return ResponseEntity.ok(documents);
    }

    @GetMapping("/file/{fileId}")
    public ResponseEntity<List<DocumentEntity>> getDocumentsByFileId(@PathVariable String fileId) {
        List<DocumentEntity> documents = documentService.findDocumentsByFileId(fileId);
        return ResponseEntity.ok(documents);
    }

    @GetMapping("/search")
    public ResponseEntity<List<DocumentEntity>> searchDocuments(
            @RequestParam(required = false) String titleKeyword,
            @RequestParam(required = false) Boolean isPublished) {
        List<DocumentEntity> documents = documentService.searchDocuments(titleKeyword, isPublished);
        return ResponseEntity.ok(documents);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDocument(@PathVariable String id) {
        documentService.deleteDocumentById(id);
        return ResponseEntity.ok().build();
    }
    // 更多API端点...

    @GetMapping("/searchPage")
    public ResponseEntity<Page<DocumentEntity>> searchDocuments(
            @RequestParam(required = false) String titleKeyword,
            @RequestParam(required = false) Boolean isPublished,
            @RequestParam(required = false) List<String> sourceSystems,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "createTime,desc") String sort) {

        // 解析排序参数
        String[] sortParams = sort.split(",");
        String property = sortParams[0];
        Sort.Direction direction = sortParams.length > 1 && "desc".equalsIgnoreCase(sortParams[1])
                ? Sort.Direction.DESC
                : Sort.Direction.ASC;

        // 创建分页对象
        Pageable pageable = PageRequest.of(page, size, Sort.by(direction, property));

        // 执行搜索
        Page<DocumentEntity> result = documentService.searchDocuments(
                titleKeyword, isPublished, sourceSystems, pageable);

        return ResponseEntity.ok(result);
    }
}
